package com.ms.orm.conditions.update;

import com.ms.common.util.CollectionUtils;
import com.ms.common.util.ReflectUtils;
import com.ms.common.util.StringUtils;
import com.ms.orm.conditions.AbstractWrapper;
import com.ms.orm.conditions.SharedString;
import com.ms.orm.conditions.segments.MergeSegments;
import com.ms.orm.constants.Constants;
import com.ms.orm.util.MetaDataOperator;
import com.ms.orm.util.sql.SqlInjectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Update 条件封装
 *
 * @author hubin miemie HCL
 * @since 2018-05-30
 */
@SuppressWarnings("serial")
public class UpdateWrapper<T> extends AbstractWrapper<T, String, UpdateWrapper<T>>
        implements Update<UpdateWrapper<T>, String> {

    /**
     * SQL 更新字段内容，例如：name='1', age=2
     */
    private final List<String> sqlSet;

    public UpdateWrapper() {
        // 如果无参构造函数，请注意实体 NULL 情况 SET 必须有否则 SQL 异常
        this(null);
    }



    public UpdateWrapper(T entity) {
        super.setEntity(entity);
        if(entity!=null){
            super.setEntityClass(ReflectUtils.getObjectClass(entity));
        }
        super.initNeed();
        this.sqlSet = new ArrayList<>();
    }

    private UpdateWrapper(T entity, List<String> sqlSet, AtomicInteger paramNameSeq,
                          Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments, SharedString paramAlias,
                          SharedString lastSql, SharedString sqlComment, SharedString sqlFirst) {
        super.setEntity(entity);
        this.sqlSet = sqlSet;
        this.paramNameSeq = paramNameSeq;
        this.paramNameValuePairs = paramNameValuePairs;
        this.expression = mergeSegments;
        this.paramAlias = paramAlias;
        this.lastSql = lastSql;
        this.sqlComment = sqlComment;
        this.sqlFirst = sqlFirst;
    }

    @Override
    public String getSqlSet() {
        if (CollectionUtils.isEmpty(sqlSet)) {
            return null;
        }
        return String.join(Constants.COMMA, sqlSet);
    }

    @Override
    public UpdateWrapper<T> set(boolean condition, String column, Object val, String mapping) {
        return maybeDo(condition, () -> {
            String sql = formatParam(mapping,column, val);
            sqlSet.add(column + Constants.EQUALS + sql);
        });
    }

    @Override
    public UpdateWrapper<T> setSql(boolean condition, String sql) {
        if (condition && StringUtils.isNotBlank(sql)) {
            sqlSet.add(sql);
        }
        return typedThis;
    }

    @Override
    protected String columnSqlInjectFilter(String column) {
        return SqlInjectionUtils.sqlInjectionReplaceBlank(column);
    }

    /**
     * 返回一个支持 lambda 函数写法的 wrapper
     */
    public LambdaUpdateWrapper<T> lambda() {
        return new LambdaUpdateWrapper<>(getEntity(), getEntityClass(), sqlSet, paramNameSeq, paramNameValuePairs,
                expression, paramAlias, lastSql, sqlComment, sqlFirst);
    }

    @Override
    protected UpdateWrapper<T> instance() {
        return new UpdateWrapper<>(getEntity(), null, paramNameSeq, paramNameValuePairs, new MergeSegments(),
                paramAlias, SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString());
    }

    @Override
    public void clear() {
        super.clear();
        sqlSet.clear();
    }

    /**
     * 获取更新语句,待占位符号
     * @return
     */
    @Override
    public String getFinalSql() {
        StringBuilder builder = new StringBuilder();
        //1、有查询条件，拼接 SELECT
        builder.append(Constants.UPDATE).append(Constants.SPACE)
                .append(MetaDataOperator.getEntityAndTableInfo(getEntityClass()).getTableName()).append(Constants.SPACE)
                .append(Constants.SET).append(Constants.SPACE)
                .append(this.getSqlSet()).append(Constants.SPACE)
                .append(Constants.WHERE).append(Constants.SPACE)
                .append(this.getExpression().getSqlSegment());
        return builder.toString();
    }


}